<?php
	

defined('_JEXEC') or die();
jimport('joomla.application.component.model');

class NBEventMapsModelNBEventMap extends JModel
{
	var $_list;

	
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	
	function _buildQuery()
	{
		$query = ' SELECT * '
			. ' FROM #__nbeventmap_marqueurs where published = 1 '
		;

		return $query;
	}

	
	function setId($id)
	{
		
		$this->_id		= $id;
		$this->_data	= null;
	}


	
	
	
	function &getData()
	{
		
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__nbeventmap '.
					'  WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->nom = null;
			$this->_data->alias = null;
			$this->_data->adresse = null;
			$this->_data->adresse2 = null;
			$this->_data->codepostal = null;
			$this->_data->ville = null;
			$this->_data->departement = null;
			$this->_data->pay = null;
			$this->_data->tel = null;
			$this->_data->tel2 = null;
			$this->_data->fax = null;
			$this->_data->email = null;
			$this->_data->web = null;
			$this->_data->img = null;
			$this->_data->album = null;
			$this->_data->intro = null;
			$this->_data->message = null;
			$this->_data->horaires_prix = null;
			$this->_data->affichage = null;
			$this->_data->marqueur = null;
			$this->_data->link = null;
			$this->_data->article_id = null;
			$this->_data->icon = null;
			$this->_data->icon_label = null;
			$this->_data->glat = null;
			$this->_data->glng = null;
			$this->_data->gzoom = null;
			$this->_data->catid = null;
			$this->_data->userid = null;
			$this->_data->published = null;
			$this->_data->checked_out = null;
			$this->_data->metadesc = null;
			$this->_data->metakey = null;
			$this->_data->ordering = 0;			
		}
		
		if (JString::strlen($this->_data->message) > 1) {
			$this->_data->text = $this->_data->intro . "<hr id=\"system-readmore\" />" . $this->_data->message;
		} else {
			$this->_data->text = $this->_data->intro;
		}
		return $this->_data;
	}

	function &getMarqueurs()
	{
		if (empty( $this->_list ))
		{
			$query = $this->_buildQuery();
			$this->_list = $this->_getList( $query );
		}

		return $this->_list;
	}

	
	function store()
	{
		$mainframe = JFactory::getApplication();
		$row =& $this->getTable();
		$data = JRequest::get( 'post' );
		
		
		
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		
		NBEventMapsHelper::saveNBEventmapPrep( $row );


		
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		
		
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		
		
		if (!$row->reorder()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		
		
			$pictures = $_FILES['files'];
			$gallery = new stdClass();

			$array_arr = array();
			for($i=0;$i< count($pictures['type']);$i++) {
				
				if (($pictures['type'][$i] == "image/gif")
				|| ($pictures['type'][$i] == "image/jpeg")
				|| ($pictures['type'][$i] == "image/png")
				|| ($pictures['type'][$i] == "image/jpg"))
				{
					$array_arr['type'][]= $pictures['type'][$i];
					$array_arr['name'][]= $pictures['name'][$i];
					$array_arr['tmp_name'][]= $pictures['tmp_name'][$i];
				}
			}
			foreach ($array_arr['name'] as $k=>$image) {
				$image_name = time() . $image;
		  		$paths = JPATH_SITE . DS . 'media' . DS . 'netbase' . DS . 'images'. DS . $image_name;
		        move_uploaded_file($array_arr['tmp_name'][$k], $paths);
		        $gallery->event_id = $row->id;
        		$gallery->name = $image_name;	

        		$gallery->id = 0;
				if (!$this->_db->insertObject( '#__nbeventmap_files', $gallery, 'id' )) {
	   		 		echo $database->stderr();
	    			return false;
	 			}

			}		

		return $row->id;
	}

	
	function getFiles()
	{
		$db =& JFactory::getDBO();
		$cid = JRequest::getVar('cid');
		if(is_array($cid)) $cid = $cid[0];
		$db->setQuery("SELECT * FROM #__nbeventmap_files WHERE event_id = '".$cid."'");
		$rows = $db->loadObjectList();
		return $rows;
	}
	function getEventId($id)
	{
		$db =& JFactory::getDBO();
		$sql = "SELECT DISTINCT(event_id) FROM #__nbeventmap_files WHERE id = ".$id;
		$db->setQuery($sql);
		$rows = $db->loadObject();
		return $rows;
	}
	function deleteFiles($id)
	{
		$db =& JFactory::getDBO();
		$paths = JPATH_SITE . DS . 'media' . DS . 'netbase' . DS . 'images'. DS;
		$qry = "SELECT name FROM #__nbeventmap_files WHERE id = ".$id;
		$db->setQuery($qry);
		$rows = $db->loadObject();
		@unlink ($paths . $rows->name);
		
		$sql = "DELETE FROM #__nbeventmap_files WHERE id = ".$id;
		$db->setQuery($sql);
		$db->query();
	}


	
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =& $this->getTable();

		if (count( $cids ))
		{
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}						
		}
		return true;
	}
	
	function move($direction)
	{
		$row =& $this->getTable();
		if (!$row->load($this->_id)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		
		if (!$row->move( $direction, ' 1 AND published >= 0 ' )) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}

	function saveorder($cid = array(), $order)
	{
		$row =& $this->getTable();
		

		
		for( $i=0; $i < count($cid); $i++ )
		{
			$row->load( (int) $cid[$i] );
			
			

			if ($row->ordering != $order[$i])
			{
				$row->ordering = $order[$i];
				if (!$row->store()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}

		
		
		$row->reorder();

		return true;
	}
}
?>
